/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-21 15:49:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-12-15 16:51:01
 * @FilePath: \ems_neutral_app\lib\pages\Mine\Fault\FaultPage.dart
 * @Description: 故障处理列表
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/bloc/userbloc/user_bloc.dart';
import 'package:ems_neutral_app/common/interfaceInstance/SmartRefresherInterfaceInstance.dart';
import 'package:ems_neutral_app/entities/WarningEntity.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:flutter/material.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../api/request/PowerStationRequest.dart';

class FaultListPage extends StatefulWidget {
  /// 默认已完成
  final bool isFinished;
  const FaultListPage({this.isFinished = false, super.key});

  @override
  State<FaultListPage> createState() => _FaultListPageState();
}

class _FaultListPageState extends State<FaultListPage>
    with SmartRefresherInterfaceInstance {
  late String _powerId;

  List _data = [];

  @override
  void initState() {
    super.initState();
    _powerId = UserBlocTool.userEntity?.powerRecordId ?? '';
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 0,
      navBarHide: true,
      stateBarHide: true,
      body: mainWidget(),
    );
  }

  mainWidget() {
    return SilSmartRefresherWidget(
        childBuild: (context, data) {
          _data = data;
          return listView();
        },
        loadDataFuture: ({manuallyRefresh, page}) {
          return api_fault_list(
              powerId: _powerId, page: page, isFinished: widget.isFinished);
        },
        interface: this);
  }

  listView() {
    return ListView.separated(
        padding: EdgeInsets.all(20.w),
        itemBuilder: (context, index) {
          return listViewItem(index);
        },
        separatorBuilder: (context, index) {
          return Container(
            height: 20.w,
          );
        },
        itemCount: _data.length);
  }

  /// 暂时不处理
  listViewItem(index) {
    WarningEntity entity = _data[index];
    return SilEmptyButton(
      padding: EdgeInsets.all(20.w),
      useConstrained: true,
      backgroundColor: ColorUtils.hexColor('#1f1f1f'),
      radius: 10.w,
      child: Column(
        children: [
          deviceInfo(entity),
          elseInfo(
              title: LangOf(context)?.faultList?.warning_desc,
              value: entity.waringInfo),
          elseInfo(
              title: LangOf(context)?.faultList?.time_failure,
              value: entity.waringTime),
          elseInfo(
              title: LangOf(context)?.faultList?.sn, value: entity.snNumber),
        ],
      ),
    );
  }

  /// 设备信息
  deviceInfo(WarningEntity entity) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SilButton(
          imgPath: 'imgs/personal/personal_dzglImg.png',
          imgHeight: 48.w,
          imgWidth: 48.w,
          imagePosition: ImagePosition.Left,
          space: 20.w,
          title: entity.powerName,
          titleColor: ColorUtils.color_font_main,
          fontSize: 17.sp,
        ),
        SilText(
          widget.isFinished
              ? LangOf(context)?.faultList?.completed
              : LangOf(context)?.faultList?.pending,
          color: widget.isFinished
              ? ColorUtils.color_theme
              : ColorUtils.color_theme_remind,
          fontSize: 17.sp,
        ),
      ],
    );
  }

  /// 其他信息
  elseInfo({String? title, String? value}) {
    return Container(
        margin: EdgeInsets.only(top: 20.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SilText(
              title,
              color: ColorUtils.color_font_main,
              fontSize: 15.sp,
            ),
            SilText(
              value,
              color: ColorUtils.color_font_main,
              fontSize: 15.sp,
            ),
          ],
        ));
  }
}
